/************************************************************************/
/* Class BarChartItem */
	function BarChartItem(name, value){
	this.Name = name;
	this.Value = value;
}

/************************************************************************/

/* Class BarGraph */
function BarGraph(id) {
	this.id = id;

	this.internalData = new Array( );

	this.graphics = new Graphics( );

	this.ValueFont = new Font('sans-serif', '9px', '#333');
	this.NameFont = new Font('sans-serif', '9px', '#333');
	this.TitleFont = new Font('sans-serif', '9px', '#333');

	this.Data = new Array( );

	this.Label = '';
	
	this.BarBorder = Border.None;
	this.GraphBorder = Border.None;
	this.GraphBackground = new ColorConvert(DotNetTypeColors.Aquamarine).Rgb( );
	this.GraphBorderColor = Color.Black;

	this.GridColor = Color.Black;
	this.ShadowColor = Color.Black;
	
	this.colorRot = new Array('#00979C', '#87BCE5', '#AEC5FF', '#00979C', '#00979C', 'Purple', 'Cyan');

	this.ShadowXOffset = 5;
	this.ShadowYOffset = 5;
	
	this.GridStyle = Border.Solid;
	
	this.ShadowColor = Color.Black;
	
	this.Height = 100;
	this.Width = 100;

	this.Title = '';
	this.XOffset = 10;

	this.BarThickness = 100;

	this.ShowGraphBackground = false;
	this.ShowGraphBorder = false;
	this.ShowShadow = false;
	this.ShowValues = false;
	this.ShowText = false;
	this.ShowTitle = false;
	this.ShowGrid = false;

	this.TitleX = 10;
	this.TitleY = 10;
}


/************************************************************************/

BarGraph.prototype.Generate = function( ) {
	var showValues = this.ShowValues;
	var showText = this.ShowText;
	var showTitle = this.ShowTitle;
	var showGrid = this.ShowGrid;

	if(arguments.length > 0) {
		if(arguments[0]) {
			showValues = arguments[0];
		}

		if(arguments[1]) {
			showText = arguments[1];
		}

		if(arguments[2]) {
			showTitle = arguments[2];
		}

		if(arguments[3]) {
			showValues = arguments[3];
		}
	}

	if(this.BarThickness == '') {
		throw 'BarThickness cannot be blank.';
	}

	if(this.ShowText && this.NameFont.Name == '') {
		throw 'NameFont cannot be blank.';
	}

	if(this.ShowValues && this.ValueFont.Name == '') {
		throw 'ValueFont cannot be blank.';
	}

	if(this.ShowTitle && this.TitleFont.Name == '') {
		throw 'TitleFont cannot be blank.';
	}

	var xOffset = 10;
	var yOffset;
	var color = 0;

	var highestValue = 0;
	var tallestFontValue = 0;
	var titleOffset = 0;
	var tallestBarHeight = 0;
	var maxY;

	if(showTitle) {
		titleOffset = parseInt(this.TitleFont.Size) + 6;

		if(this.TitleFont == '') {
			throw 'TitleFont cannot be blank.';
		}

		this.graphics.Font = this.TitleFont;
	}

	maxY = parseInt((this.graphics.Height * 0.80) - titleOffset);

	for(var itemName in this.Data) {
		var item = this.Data[itemName];

		if(parseInt(item.Value) > highestValue) {
			highestValue = parseInt(item.Value);
			tallestBarHeight = parseInt((parseInt(item.Value) * maxY / highestValue));
		}

		if(parseInt(this.ValueFont.Size) > tallestFontValue) {
			tallestFontValue = parseInt(this.ValueFont.Size);
		}
	}

	itemName = null;
	item = null;

	yOffset = parseInt(((this.graphics.Height - maxY) - tallestFontValue * 1.4));


	if(this.ShowGraphBorder) {
		this.internalData.push(this.graphics.drawRect(0, 0, this.graphics.Width, this.graphics.Height + 5, this.GraphBackground, this.GraphBorder, this.GraphBorderColor, 50, this.id + 'BackgroundShow', this.Title));
	}
	else {
		this.internalData.push(this.graphics.drawRect(0, 0, this.graphics.Width, this.graphics.Height + 5, this.GraphBackground, Border.None, Color.Transparent, 50, this.id + 'BackgroundNoShow', this.Title));
	}

	if(this.ShowGrid) {
		multiplier = parseInt(tallestBarHeight / 10);
		for(var n = yOffset + maxY + 1; n > titleOffset + this.TitleY; n -= multiplier) {
			this.internalData.push(this.graphics.drawHorizontalLine(0, n, this.graphics.Width + 1, this.GridColor, this.GridStyle, 50, this.id + 'Background3' + n));
		}
	}

	for(itemName in this.Data) {
		item = this.Data[itemName];

		color++;
		if(color + 1 > this.colorRot.Length) {
			color = 0;
		}

		var barHeight = parseInt((parseInt(item.Value) * maxY / highestValue));

		if(showValues) {
			this.graphics.Font = this.ValueFont;
			this.Data[itemName].obj = this.graphics.drawRect(xOffset, yOffset + maxY - barHeight, xOffset + parseInt(this.BarThickness), yOffset + maxY - barHeight + barHeight, this.colorRot[color - 1], this.BarBorder, Color.Default, 100, this.id + 'Value' + item.Value, item.Value + this.Label);
		}
		else {
			this.Data[itemName].obj = this.graphics.drawRect(xOffset, yOffset + maxY - barHeight, xOffset + parseInt(this.BarThickness), yOffset + maxY - barHeight + barHeight, this.colorRot[color - 1], this.BarBorder, Color.Default, 100, this.id + 'Value' + item.Value);
		}
		
		if(this.ShowShadow) {
			this.Data[itemName].objShadow = this.graphics.drawRect(xOffset + this.ShadowXOffset, yOffset + maxY - barHeight + this.ShadowYOffset, xOffset + parseInt(this.BarThickness) + this.ShadowXOffset, yOffset + maxY - barHeight + barHeight + this.ShadowYOffset, this.ShadowColor, Border.None, this.ShadowColor, 99, this.id + 'Shadow');
		}

		if(showText) {
			this.graphics.Font = this.NameFont;
			this.Data[itemName].objText = this.graphics.drawStringArea(itemName, xOffset, yOffset + maxY + this.ShadowYOffset, parseInt(this.BarThickness), 30, this.id + 'Name' + itemName);
		}
		
		xOffset += parseInt(this.BarThickness) + this.XOffset;
	}

	itemName = null;

	var newBox = document.createElement('div');

	for(itemName in this.Data) {
		newBox.appendChild(this.Data[itemName].obj);
		
		if(this.Data[itemName].objText) {
			newBox.appendChild(this.Data[itemName].objText);
		}

		if(this.Data[itemName].objShadow) {
			newBox.appendChild(this.Data[itemName].objShadow);
		}
	}

	for (i = 0; i < this.internalData.length; i++) {
		newBox.appendChild(this.internalData[i]);
	}
	
	newBox.appendChild(this.graphics.drawRect(0, 0, this.graphics.Width, this.graphics.Height, Border.Transparent, Border.None, Color.Transparent, 1000, this.id + 'Cover', ''));

	newBox.id = this.id;
	newBox.className = 'Absolute';

	this.Obj = newBox;
}

BarGraph.prototype.Attach = function(objParent) {
	objParent.insertBefore(this.Obj, objParent.firstChild);
}

BarGraph.prototype.Add = function(item) {
	this.Data[item.Name] = new Object( );
	this.Data[item.Name].Value = item.Value;
}

BarGraph.prototype.SetHeight = function(height) {
	this.graphics.Height = height;
}

BarGraph.prototype.SetWidth = function(width) {
	this.graphics.Width = width;
}

BarGraph.prototype.SetTitleLocation = function(x, y) {
	this.TitleX = x;
	this.TitleY = y;
}

BarGraph.prototype.SetLocation = function(x, y) {
	this.Obj.style.left = x + 'px';
	this.Obj.style.top = y + 'px';
}